using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DreamBuilder.UI.UserControls.DataAccess;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.DictionaryAccess;


namespace DreamBuilder.UI.UserControls
{
    public class CboTemplateColumn : IChildItem
    {
        private UserControl userControl;
        private ChildTableItemInfo childTableItemInfo;
        DDLReference ddlReference = null;
        public event ItemSelectedEventHandler ItemSelected;

        public CboTemplateColumn()
        {

        }

        public CboTemplateColumn(ChildArgs childArgs)
        {
            userControl = childArgs.Container;
            this.childTableItemInfo = childArgs.CChildTableItemInfo;
        }
        public void SetControlID()
        {
            ddlReference.ID = "cbo_" + this.childTableItemInfo.FieldCode;
        }
        public void SetControlAttributes()
        {
            ddlReference.TableCode = UChildCommon.GetChildReferenceObj(childTableItemInfo);
            ddlReference.Required = (!childTableItemInfo.Nullable);
            ddlReference.Enabled = (!childTableItemInfo.ReadOnly);
            ddlReference.Width = Unit.Percentage(100);
            ddlReference.AutoPostBack = false;
            if (childTableItemInfo.AutoPostBack == true)
            {
                ddlReference.AutoPostBack = true;
                ddlReference.RefSelected += new RefSelectHandler(pDDLReference_RefSelected);
            }
            if (!string.IsNullOrEmpty(childTableItemInfo.SelectRangeSQL))
            {
                ddlReference.WhereSql = ddlReference.WhereSql == "" ? childTableItemInfo.SelectRangeSQL : ("(" + childTableItemInfo.SelectRangeSQL + ") and (" + ddlReference.WhereSql + ")");
            }
        }
        public void SetControlValue(GridViewRow gvRow, string controlID, FieldDicInfo fieldDicInfo, DataRow dr)
        {

            DDLReference ddlReference = gvRow.FindControl(controlID) as DDLReference;
            if (ddlReference != null)
            {
                ddlReference.Attributes.Add("RowIndex", gvRow.RowIndex.ToString());
                ddlReference.Attributes.Add("FieldCode", fieldDicInfo.FieldCode);
                ddlReference.RefCodeValue = dr[fieldDicInfo.FieldCode].ToString();

            }

        }

        public void GetControlValue(GridViewRow gvRow, DataRow dr, string controlID, ChildTableItemInfo fieldDicInfo)
        {
            DDLReference currDDLReference = gvRow.FindControl(controlID) as DDLReference;
            if (currDDLReference != null)
            {
                dr[fieldDicInfo.FieldCode] = currDDLReference.RefCodeValue.Length == 0 ? System.DBNull.Value : (object)currDDLReference.RefCodeValue;
            }
        }

        public void InstantiateIn(Control container)
        {
            ddlReference = (DDLReference)userControl.LoadControl("DDLReference.ascx");
            SetControlID();
            SetControlAttributes();
            container.Controls.Add(ddlReference);
        }

        protected void pDDLReference_RefSelected(object sender, EventArgs e)
        {
            DDLReference ddlReference = sender as DDLReference;
            GridViewRow gvRow = ddlReference.NamingContainer as GridViewRow;
            GridView gvChild = gvRow.NamingContainer as GridView;
            UCChild ucChild = gvChild.Parent as UCChild;
            DataTable curentData = ucChild.OutPutDataTable;
            int index = int.Parse(ddlReference.Attributes["RowIndex"]);
            object key = gvChild.DataKeys[index][0];
            DataRow row = curentData.Rows.Find(key);
            row[ddlReference.Attributes["FieldCode"].ToString()] = ddlReference.RefCodeValue;
            ucChild.Refresh(ref curentData);
            if (ddlReference != null && gvRow != null)
            {
                ChildItemEventArgs childItemEventArgs = new ChildItemEventArgs();
                childItemEventArgs.CurrentEventItem = gvRow;
                if (ItemSelected != null)
                {
                    ItemSelected(ddlReference, childItemEventArgs);
                }
            }
        }
    }
}

